package cn.stylefeng.guns.modular.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.modular.biz.pojo.response.PmPowerplantProConBomGroupByDateVo;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.biz.entity.PmPowerplantProConBom;
import cn.stylefeng.guns.modular.biz.enums.PmPowerplantProConBomExceptionEnum;
import cn.stylefeng.guns.modular.biz.mapper.PmPowerplantProConBomMapper;
import cn.stylefeng.guns.modular.biz.pojo.request.PmPowerplantProConBomRequest;
import cn.stylefeng.guns.modular.biz.service.PmPowerplantProConBomService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * MES投煤量数据业务实现层
 *
 * @author 张朋飞
 * @since 2024/10/08 17:03
 */
@Service
public class PmPowerplantProConBomServiceImpl extends ServiceImpl<PmPowerplantProConBomMapper, PmPowerplantProConBom> implements PmPowerplantProConBomService {

	@Override
    public void add(PmPowerplantProConBomRequest pmPowerplantProConBomRequest) {
        PmPowerplantProConBom pmPowerplantProConBom = new PmPowerplantProConBom();
        BeanUtil.copyProperties(pmPowerplantProConBomRequest, pmPowerplantProConBom);
        this.save(pmPowerplantProConBom);
    }

    @Override
    public void del(PmPowerplantProConBomRequest pmPowerplantProConBomRequest) {
        PmPowerplantProConBom pmPowerplantProConBom = this.queryPmPowerplantProConBom(pmPowerplantProConBomRequest);
        this.removeById(pmPowerplantProConBom.getPppcbId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(PmPowerplantProConBomRequest pmPowerplantProConBomRequest) {
        this.removeByIds(pmPowerplantProConBomRequest.getBatchDeleteIdList());
    }

    @Override
    public void edit(PmPowerplantProConBomRequest pmPowerplantProConBomRequest) {
        PmPowerplantProConBom pmPowerplantProConBom = this.queryPmPowerplantProConBom(pmPowerplantProConBomRequest);
        BeanUtil.copyProperties(pmPowerplantProConBomRequest, pmPowerplantProConBom);
        this.updateById(pmPowerplantProConBom);
    }

    @Override
    public PmPowerplantProConBom detail(PmPowerplantProConBomRequest pmPowerplantProConBomRequest) {
        return this.queryPmPowerplantProConBom(pmPowerplantProConBomRequest);
    }

    @Override
    public PageResult<PmPowerplantProConBom> findPage(PmPowerplantProConBomRequest pmPowerplantProConBomRequest) {
        LambdaQueryWrapper<PmPowerplantProConBom> wrapper = createWrapper(pmPowerplantProConBomRequest);
        Page<PmPowerplantProConBom> pageList = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(pageList);
    }

    @Override
    public PmPowerplantProConBomGroupByDateVo listGroupDate(String gatherDate) {
        return this.baseMapper.listGroupDate(gatherDate);
    }

    @Override
    public List<PmPowerplantProConBom> findList(PmPowerplantProConBomRequest pmPowerplantProConBomRequest) {
        LambdaQueryWrapper<PmPowerplantProConBom> wrapper = this.createWrapper(pmPowerplantProConBomRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author 张朋飞
     * @since 2024/10/08 17:03
     */
    private PmPowerplantProConBom queryPmPowerplantProConBom(PmPowerplantProConBomRequest pmPowerplantProConBomRequest) {
        PmPowerplantProConBom pmPowerplantProConBom = this.getById(pmPowerplantProConBomRequest.getPppcbId());
        if (ObjectUtil.isEmpty(pmPowerplantProConBom)) {
            throw new ServiceException(PmPowerplantProConBomExceptionEnum.PM_POWERPLANT_PRO_CON_BOM_NOT_EXISTED);
        }
        return pmPowerplantProConBom;
    }

    /**
     * 创建查询wrapper
     *
     * @author 张朋飞
     * @since 2024/10/08 17:03
     */
    private LambdaQueryWrapper<PmPowerplantProConBom> createWrapper(PmPowerplantProConBomRequest pmPowerplantProConBomRequest) {
        LambdaQueryWrapper<PmPowerplantProConBom> queryWrapper = new LambdaQueryWrapper<>();

        Long pppcbId = pmPowerplantProConBomRequest.getPppcbId();
        queryWrapper.eq(ObjectUtil.isNotNull(pppcbId), PmPowerplantProConBom::getPppcbId, pppcbId);

        String gatherDate = pmPowerplantProConBomRequest.getGatherDate();
        queryWrapper.eq(ObjectUtil.isNotNull(gatherDate), PmPowerplantProConBom::getGatherDate, gatherDate);

        String amountIn = pmPowerplantProConBomRequest.getAmountIn();
        queryWrapper.like(ObjectUtil.isNotEmpty(amountIn), PmPowerplantProConBom::getAmountIn, amountIn);


        return queryWrapper;
    }

}
